// 经纬度获取地形高度
export function getTerrHightByDegree(viewer, lgtd, lttd) {
    return new Promise((resolve, reject) => {
        if (viewer.scene.terrainProvider.availability) {
            var pos = [Cesium.Cartographic.fromDegrees(Number(lgtd), Number(lttd))]

            var promise = Cesium.sampleTerrainMostDetailed(viewer.scene.terrainProvider, pos)
            promise.then(upos => {
                resolve(upos[0].height)
            })
        } else {
            resolve(0)
        }
    })
}
// 文字转图片
export function getTxtImg(txt, size, font, hasBack) {
    return new Promise((resolve, reject) => {
        var canvas = document.createElement('canvas')
        canvas.width = (txt.length + 1) * size * 2 // + 1有的字体较宽
        canvas.height = size * 2.5// + 0.5有的字体较宽
        var ctx = canvas.getContext('2d')
        ctx.scale(2, 2) // 清晰一些
        if (hasBack) {
            ctx.moveTo(0, 0)
            ctx.lineTo(canvas.width / 2, 0)
            ctx.lineTo(canvas.width / 2, canvas.height / 2)
            ctx.lineTo(0, canvas.height / 2)

            ctx.closePath()
            ctx.fillStyle = '#3d6f7b'
            ctx.lineWidth = 3
            ctx.strokeStyle = '#00faff'
            ctx.fill()
            ctx.stroke()
        }
        ctx.shadowOffsetX = -5 // 用来设定阴影在 X轴的延伸距
        ctx.shadowOffsetY = 5 // 用来设定阴影在 Y轴的延伸距
        // ctx.shadowBlur = 0 // 设定阴影的模糊程度 默认0
        // ctx.shadowColor = 'rgba(0, 0, 0, 0.85)' // 设定阴影颜色效果
        ctx.font = `bold ${size}px ${font}`
        ctx.textAlign = 'center'
        ctx.textBaseline = 'bottom'
        ctx.fillStyle = '#FFF'
        // if (hasBack) {
        //     ctx.shadowColor = 'rgba(255, 255,255, 0.85)' // 设定阴影颜色效果
        //     ctx.fillStyle = '#000'
        // }
        ctx.fillText(txt, (txt.length + 1) * size * 0.5, size * 1.25)

        const image = new Image()
        image.src = canvas.toDataURL('image/png')
        resolve(image)
    })
}
export function cartesianToWGS84(cartesian) {
    var ellipsoid = Cesium.Ellipsoid.WGS84
    var cartographic = ellipsoid.cartesianToCartographic(cartesian)
    return {
        lng: Cesium.Math.toDegrees(cartographic.longitude),
        lat: Cesium.Math.toDegrees(cartographic.latitude),
        alt: cartographic.height
    }
}
export function wgs84ToCartesian(cartesian) {
    var ellipsoid = Cesium.Ellipsoid.WGS84
    var cartographic = ellipsoid.cartesianToCartographic(cartesian)
    return {
        lng: Cesium.Math.toDegrees(cartographic.longitude),
        lat: Cesium.Math.toDegrees(cartographic.latitude),
        alt: cartographic.height
    }
}

export function getDistance(lng1, lat1, lng2, lat2) {
    var radian1 = (lat1 * Math.PI) / 180.0 // 纬度：角度转换成弧度
    var radian2 = (lat2 * Math.PI) / 180.0
    var radianLatDiff = radian1 - radian2 // 纬度之差
    var radianLonDiff = (lng1 * Math.PI) / 180.0 - (lng2 * Math.PI) / 180.0 // 经度之差
    var radius = 6378137 // 赤道半径
    return (
        radius *
        2 *
        Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(radianLatDiff / 2), 2) +
                Math.cos(radian1) * Math.cos(radian2) * Math.pow(Math.sin(radianLonDiff / 2), 2)
            )
        )
    )
}
export function getDisFromDegreesArray(lonlats) {
    var res = 0
    for (var i = 0; i < lonlats.length / 2; i = i + 2) {
        var dis = getDistance(lonlats[i], lonlats[i + 1], lonlats[i + 2], lonlats[i + 3])
        res += dis
    }

    return res.toFixed(0)
}

// 定义两点连线在XY平面角度
// 角度 = 弧度 * 180 / Math.PI;
// 弧度= 角度 * Math.PI / 180;  90 => 1.5707963267948966
export function angle_p2p(a, b) {
    var dx = b[0] - a[0]
    var dy = b[1] - a[1]
    var ang = Math.atan(dy / dx)
    if ((dx < 0 && dy < 0) || (dx < 0 && dy > 0)) ang += Math.PI
    if ((dx < 0) && (dy === 0.0)) ang += Math.PI
    return ang // 弧度
}

export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result

    const later = function() {
        // 据上一次触发时间间隔
        const last = +new Date() - timestamp

        // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
        if (last < wait && last > 0) {
            timeout = setTimeout(later, wait - last)
        } else {
            timeout = null
            // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
            if (!immediate) {
                result = func.apply(context, args)
                if (!timeout) context = args = null
            }
        }
    }

    return function(...args) {
        context = this
        timestamp = +new Date()
        const callNow = immediate && !timeout
        // 如果延时不存在，重新设定延时
        if (!timeout) timeout = setTimeout(later, wait)
        if (callNow) {
            result = func.apply(context, args)
            context = args = null
        }

        return result
    }
}
